using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Utility;
using Entities;

public partial class Admin_Danhsachdiadanh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {
          
            LoadData();
          
        }
    }
  
    private void LoadData()
    {
        pager.PageSize = 5;
        Criteria cri = new Criteria();
        int total = 0;
        if (txttendd != null)
            cri.add(Condition.AND, tbDiadanhCollumn.tendiadanh .ToString(), Condition.LIKE, txttendd.Text);

        tbDiadanhCollection  ddanhCollection = tbDiadanhBUS .Get(cri.Criter, pager.CurrentIndex, pager.PageSize,
       tbDiadanhCollumn .madiadanh .ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = ddanhCollection;
        Repeater1.DataBind();

        pager.ItemCount = total;

    }

    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = Convert.ToInt32(e.CommandArgument);
        LoadData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item
        || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            tbDiadanh  ddanh = (tbDiadanh )e.Item.DataItem;

              tbDiadiem ddiem = tbDiadiemBUS.GetByID(ddanh.Madiadiem);
              if (ddiem != null)
              {
                  Literal lit_ddiem = (Literal)e.Item.FindControl("lit_tenddiem");
                  lit_ddiem.Text = ddiem.Tendiadiem;
              }
    

            Literal  hp_ten = (Literal )e.Item.FindControl("hp_tendd");
            hp_ten.Text = ddanh .Tendiadanh ;

            Literal lit_mota = (Literal)e.Item.FindControl("lit_mota");
            lit_mota.Text = ddanh.Motadiadanh;


            HyperLink hp_sua = (HyperLink)e.Item.FindControl("hp_sua");
            hp_sua .NavigateUrl = "~/Admin/Suadiadanh.aspx?id=" + ddanh .Madiadanh .ToString();

            Image img_ddanh = (Image)e.Item.FindControl("img_ddanh");
            img_ddanh.ImageUrl = "~/Hinhanh/Hinh/" + ddanh.Hinhdiadanh ;

          
          

        }
    }
    protected void lbt_xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_xoa");
            if (cb_xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                tbDiadanh  ddanh= tbDiadanhBUS .GetByID(id);
           
                if (ddanh .Hinhdiadanh  != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/" + ddanh .Hinhdiadanh ));
                    System.IO.File.Delete(Server.MapPath("~/Hinhanh/Hinh/" + ddanh .Hinhdiadanh ));
                }
                tbDiadanhBUS .Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}
